//
//  MController.m
//  Mapper
//
//  Created by Steven Troughton-Smith on 30/06/2007.
//  Copyright 2007 __MyCompanyName__. All rights reserved.
//

#import "MController.h"

NSModalSession session;

@implementation MController

-(IBAction)cancelDropPin:(id)sender
{
	[[NSApplication sharedApplication] abortModal];
	[[NSApplication sharedApplication] endSheet:markerWindow];
	[markerWindow close];
}

-(IBAction)commitDropPin:(id)sender
{
	[[NSApplication sharedApplication] endSheet:markerWindow];
	[markerWindow close];
	
	[scriptObject callWebScriptMethod:@"dropPin" withArguments:[NSArray arrayWithObject:[markerTitleField stringValue]]];
}

-(IBAction)dropPin:(id)sender
{
	[[NSApplication sharedApplication] beginSheet:markerWindow modalForWindow:mainWindow modalDelegate:self didEndSelector:nil contextInfo:nil];
}

-(IBAction)search:(id)sender
{
	[scriptObject callWebScriptMethod:@"searchMe" withArguments:[NSArray arrayWithObject:[sender stringValue]]];
}

-(IBAction)changeView:(id)sender
{

	switch ([sender selectedSegment])
	{
		case 0:
		{
			[scriptObject callWebScriptMethod:@"useStandardMap" withArguments:nil];
			break;
		}
		case 1:
		{
			[scriptObject callWebScriptMethod:@"useSatelliteMap" withArguments:nil];
			break;
		}
		case 2:
		{
			[scriptObject callWebScriptMethod:@"useHybridMap" withArguments:nil];
			break;
		}
	}
}

-(IBAction)setZoomLevel:(id)sender
{
	[scriptObject callWebScriptMethod:@"setZoom" withArguments:[NSArray arrayWithObject:[NSNumber numberWithFloat:[sender floatValue]]]];
}

-(void)refreshZoomSlider:(NSNumber *)val
{
	[zoomSlider setFloatValue:[val floatValue]];
}

-(void)log:(id)s
{
	NSLog([s description]);
}

-(void)retreiveNewPinLocation:(WebScriptObject *)pin
{
	// 0 = lat, 1 = lng, 2 = title

	NSDictionary *pinCoords = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:[pin webScriptValueAtIndex:0], [pin webScriptValueAtIndex:1], nil] forKeys:[NSArray arrayWithObjects:@"Latitude", @"Longtitude", nil]];
	NSDictionary *newPin = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:[NSArray arrayWithObject:pinCoords],[pin webScriptValueAtIndex:2],nil] forKeys:[NSArray arrayWithObjects:@"Location", @"Title", nil]];

	for (NSDictionary *currentPin in  droppedPins)
	{

//		NSNumber *lat = [[[currentPin objectForKey:@"Location"] objectAtIndex:0] objectForKey:@"Latitude"];
//		NSNumber *lng = [[[currentPin objectForKey:@"Location"] objectAtIndex:0] objectForKey:@"Longtitude"];
		NSString *title = [currentPin objectForKey:@"Title"];
		
		if ([title isEqualToString:[pin webScriptValueAtIndex:2]])
		[droppedPins removeObject:currentPin];

	}

	[droppedPins addObject:newPin];
	[droppedPins writeToFile:droppedPinsPath atomically:NO];
}

-(IBAction)loadBookmarkedPins:(id)sender
{
	NSLog([droppedPins description]);
	
	for (NSDictionary *pin in  droppedPins)
	{
		
		NSNumber *lat = [[[pin objectForKey:@"Location"] objectAtIndex:0] objectForKey:@"Latitude"];
		NSNumber *lng = [[[pin objectForKey:@"Location"] objectAtIndex:0] objectForKey:@"Longtitude"];
		NSString *title = [pin objectForKey:@"Title"];
		
		[scriptObject callWebScriptMethod:@"loadPin" withArguments:[NSArray arrayWithObjects:lat, lng, title, nil]];

	}

}

-(void)awakeFromNib
{
	[_searchToolbarItem setMaxSize:NSMakeSize(10000,23)];
	[_searchToolbarItem setMinSize:NSMakeSize(200,23)];

	NSString *p = [NSString stringWithFormat:@"file://%@/%@",[[[NSBundle mainBundle] resourcePath] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding], @"map.html"];

	[[mapView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:p]]];

	scriptObject = [mapView windowScriptObject];
	[scriptObject setValue:self forKey:@"cocoaApplication"];
	
	droppedPinsPath = [[[NSString stringWithString:@"~/Library/Preferences/com.mapper.markers.plist"] stringByExpandingTildeInPath] retain];

	if ([[NSFileManager defaultManager] fileExistsAtPath:droppedPinsPath])
		droppedPins = [[NSMutableArray arrayWithContentsOfFile:droppedPinsPath] retain];
	else
		droppedPins = [[NSMutableArray array] retain];
		
	

}

+ (BOOL)isSelectorExcludedFromWebScript:(SEL)aSelector
{
	return NO;
}

@end
